import { ILoadMapTypeAll } from '@/common';
import { getBaseUrl } from '@/services/urlconst';
import { getTenantID, requestFunComponFiex } from '@/utils';
import { message } from 'antd';
import { useMemo } from 'react';
import { useRequest } from 'umi';

let baseUrl = getBaseUrl();

/**
 * 获取新增流程相关数据
 * @returns
 * 
 */
const getFlowRelevantService = () => {
  const getFlowRelevant = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/activitiController/getFlowRelevant`;
      let bodyParams = {
        ...params,
        orderType: `${getTenantID()}${ILoadMapTypeAll?.ALLORDER_TYPE}`,
        flowType: `${getTenantID()}${ILoadMapTypeAll?.ALLFLOW_TYPE}`,
        checkType: `${getTenantID()}${ILoadMapTypeAll?.ALLFLOW_CHECKRESULT}`,
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        if (!(res && res?.msgFlag)) {
          message.error(res?.msg);
        }
      },
    },
  );

  const data = useMemo(() => {
    return getFlowRelevant?.data?.obj;
  }, [getFlowRelevant?.data]);

  return {
    getFlowRelevantRun: getFlowRelevant?.run,
    getFlowRelevantLoading: getFlowRelevant?.loading,
    getFlowRelevantData: data,
  };
};

/**
 * 新增流程类别
 * @param modelTypeAddCallback
 * @returns
 */
const modelTypeAddService = (modelTypeAddCallback: (res: any) => void) => {
  const modelTypeAdd = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/activitiController/modelTypeAdd`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: modelTypeAddCallback,
    },
  );

  return {
    modelTypeAddRun: modelTypeAdd?.run,
    modelTypeAddLoading: modelTypeAdd?.loading,
  };
};

/**
 * 编辑流程类别
 * @param modelTypeEditCallback
 * @returns
 */
const modelTypeEditService = (modelTypeEditCallback: (res: any) => void) => {
  const modelTypeEdit = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/activitiController/modelTypeEdit`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('PUT', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: modelTypeEditCallback,
    },
  );

  return {
    modelTypeEditRun: modelTypeEdit?.run,
    modelTypeEditLoading: modelTypeEdit?.loading,
  };
};

/**
 * 修改流程状态
 * @param modifyStateCallback
 * @returns
 */
const modifyStateService = (modifyStateCallback: (res: any) => void) => {
  const modifyState = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/activitiController/modifyState`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('PUT', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: modifyStateCallback,
    },
  );

  return {
    modifyStateRun: modifyState?.run,
    modifyStateLoading: modifyState?.loading,
  };
};

/**
 * 查看流程图
 * @param getProcdefImageCallback
 * @returns
 */
const getProcdefImageService = () => {
  const getProcdefImage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/activitiController/getProcdefImage/${params?.procDefID}`;

      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: () => { },
    },
  );

  const data = useMemo(() => {
    return getProcdefImage?.data;
  }, [getProcdefImage?.data]);

  return {
    getProcdefImageRun: getProcdefImage?.run,
    getProcdefImageLoading: getProcdefImage?.loading,
    getProcdefImageData: data,
  };
};

export {
  getFlowRelevantService,
  modelTypeAddService,
  modelTypeEditService,
  modifyStateService,
  getProcdefImageService,
};
